package flex.cc.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import flex.cc.base.exception.BusinessException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author wkangle
 * @className CommonUtils
 * @description 公共方法类
 * @time 2021-09-06 17:59:13
 */
@Component
public class CommonUtils {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    public static final String ALLOT_RESCUE_URL = "https://rescue.test.pengdajinrong.com/openapi/createRescue"; // 下游下单接口url，暂时写死，以后配置到数据库
    public static final String CANCEL_RESCUE_URL = "https://rescue.test.pengdajinrong.com/openapi/cancelRescue"; // 下游取消接口url，暂时写死，以后配置到数据库
    public static final String URGE_RESCUE_URL = "https://rescue.test.pengdajinrong.com/openapi/urgingRescue"; // 下游催单接口url
    public static final String SYN_STATUS_URL = "http://1.202.235.87:9001/CCHttpServiceINT/servlet/rescueOperationOptional"; // 上游状态回写接口url
    public static final String UPLOAD_PATH = "D:\\image\\"; // 图片回传，车服系统保存的根位置
    /**
     * 请求上游所需的两个参数值
     */
    public static final String PARTNERCODE = "HYJY";
    public static final String CLIENTID = "SUN_HYJY_RESCUE";

    public static String getCurrentTime(){
        String strCurrentDateTime = "";
        Date currentDateTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        strCurrentDateTime = formatter.format(currentDateTime);
        return strCurrentDateTime;
    }

    public static String doPost(String url, String body){
    	long timestamp = System.currentTimeMillis();
    	logger.info("救援请求内容==>> Time:{}, Url:{}, Body:{}", timestamp, url, body);
        String result = HttpClientUtil.sendHttpPost(url, body, new HashMap<String, String>(){{
            put("Content-Type", "application/json;charset=UTF-8");
            put("Request-Id", UUID.randomUUID().toString().replaceAll("-", ""));
            put("Authorization", OpenApiConf.Authorization);
            put("Timestamp", String.valueOf(timestamp));
            put("Signature", OpenApiUtil.sign(OpenApiUtil.readPrivateKey(OpenApiConf.SC_PrivateKy), timestamp + "\n" + body));
        }});
        logger.info("救援请求结果==>> Time:{}, Result:{}", timestamp, result);
        return result;
    }
    
    //到店类服务使用的post请求
    @SuppressWarnings("serial")
	public static String storePost(String url, String body){
    	long timestamp = System.currentTimeMillis();
    	logger.info("到店类服务请求内容==>> Time:{}, Url:{}, Body:{}", timestamp, url, body);
        String result = HttpClientUtil.sendHttpPost(url, body, new HashMap<String, String>(){{
            put("Content-Type", "application/json;charset=UTF-8");
        }});
        logger.info("到店类服务请求结果==>> Time:{}, Result:{}", timestamp, result);
        return result;
    }
    
    //代驾类服务使用的post请求
    @SuppressWarnings("serial")
	public static String driverPost(String url, String body){
    	long timestamp = System.currentTimeMillis();
    	logger.info("代驾类服务请求内容==>> Time:{}, Url:{}, Body:{}", timestamp, url, body);
        String result = HttpClientUtil.sendHttpPost(url, body, new HashMap<String, String>(){{
            put("Content-Type", "application/json;charset=UTF-8");
        }});
        logger.info("代驾类服务请求结果==>> Time:{}, Result:{}", timestamp, result);
        return result;
    }
    

    public static String toJSONString(Object body) {
        try {
            return (new ObjectMapper()).setSerializationInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(body);
        } catch (JsonProcessingException e) {
            logger.error("数据格式错误: ", e.getStackTrace());
            throw new BusinessException("数据格式错误");
        }
    }

    /**
	 *
	 * @author WANGCONG
	 * 2021年9月10日下午7:01:56
	 * @desc 数字字符串比较大小
	 */
    public static boolean compareDX(String s1, String s2){
		if(StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2)){
			return false;
		}
    	return Integer.parseInt(s1)>Integer.parseInt(s2);
    }

}
